package com.liuwa.knowledge.controller;

import java.util.List;

import com.liuwa.common.utils.bean.BeanUtils;
import com.liuwa.common.exception.ServiceException;
import com.liuwa.common.utils.poi.ExportResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.liuwa.common.annotation.Log;
import com.liuwa.common.core.controller.BaseController;
import com.liuwa.common.enums.BusinessType;
import com.liuwa.knowledge.domain.Publisher;
import com.liuwa.knowledge.query.PublisherQuery;

import com.liuwa.knowledge.service.PublisherService;
import com.liuwa.common.utils.poi.ExcelUtil;
import com.liuwa.common.bean.Page;

/**
 * 出版社Controller
 * 
 * @author liuwa
 * @date 2025-03-31 22:30:11
 */
@RestController
@Api(value = "出版社")
@RequestMapping("/knowledge/publishers")
public class PublisherController extends BaseController{

    @Autowired
    private PublisherService publisherService;

    /**
     * 查询出版社列表
     */
    @ApiOperation("查询出版社列表")
    @PreAuthorize("@ss.hasPermi('knowledge:publisher:list')")
    @GetMapping
    public Page<Publisher> list(PublisherQuery query)
    {
        Publisher condition = new Publisher();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        startPage(query);
        List<Publisher> list = publisherService.findList(condition);
        return getPage(list, query);
    }


    /**
     * 导出出版社列表
     */
    @ApiOperation("导出出版社")
    @PreAuthorize("@ss.hasPermi('knowledge:publisher:export')")
    @Log(title = "导出出版社", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ExportResult export(PublisherQuery query)
    {
        Publisher condition = new Publisher();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        List<Publisher> list = publisherService.findList(condition);
        ExcelUtil<Publisher> util = new ExcelUtil<Publisher>(Publisher.class);
        return util.exportExcel(list, "出版社数据");
    }

    /**
     * 获取出版社详细信息
     */
    @ApiOperation("获取出版社详细信息")
    @PreAuthorize("@ss.hasPermi('knowledge:publisher:query')")
    @GetMapping(value = "/{id}")
    public Publisher get(@PathVariable("id") String id)
    {
        return publisherService.get(id);
    }

    /**
     * 新增出版社
     */
    @ApiOperation("新增出版社")
    @PreAuthorize("@ss.hasPermi('knowledge:publisher:add')")
    @Log(title = "新增出版社", businessType = BusinessType.INSERT)
    @PostMapping
    public void add(@RequestBody Publisher publisher)
    {
        publisherService.insert(publisher);
    }

    /**
     * 修改出版社
     */
    @ApiOperation("修改出版社")
    @PreAuthorize("@ss.hasPermi('knowledge:publisher:edit')")
    @Log(title = "修改出版社", businessType = BusinessType.UPDATE)
    @PutMapping
    public void edit(@RequestBody Publisher publisher)
    {
        Publisher oldPublisher = publisherService.get(publisher.getId());
        if(oldPublisher == null){
            throw new ServiceException("操作的出版社不存在或已删除");
        }
        BeanUtils.copyBeanNotNull2Bean(publisher, oldPublisher);
        publisherService.update(oldPublisher);
    }



    /**
     * 删除出版社
     */
    @ApiOperation("删除出版社")
    @PreAuthorize("@ss.hasPermi('knowledge:publisher:remove')")
    @Log(title = "删除出版社", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public void remove(@PathVariable String[] ids)
    {
		publisherService.batchDeleteByLogic(ids);
    }
	
	
	
    /**
     * 查询出版社已删除列表
     */
    @ApiOperation("查询出版社已删除列表")
    @PreAuthorize("@ss.hasPermi('knowledge:publisher:trash')")
    @GetMapping("/deleted")
    public Page<Publisher> deleted(PublisherQuery query)
    {
        Publisher condition = new Publisher();
        BeanUtils.copyBeanNotNull2Bean(query, condition);
        startPage();
        List<Publisher> list = publisherService.findDeletedList(condition);
        return getPage(list, query);
    }



    /**
     * 恢复出版社
     */
    @ApiOperation("恢复出版社")
    @PreAuthorize("@ss.hasPermi('knowledge:publisher:restore')")
    @Log(title = "恢复出版社", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}/restore")
    public void restore(@PathVariable String[] ids)
    {
        publisherService.batchRestore(ids);
    }
}
